/**
 * @description  权益行业风险分析
 * @author zls
 */
import React, { PureComponent } from 'react';
import { Row, Col, message, Tabs } from 'antd';
import { DATA_ENTRY, NoQueryC, AdvancedSearch, ContentPackage } from '@/src/comps';
import { connect } from 'dva';
import moment from 'moment';
import IndustryExposure from './IndustryExposure';
import { formSelectData } from '../../../../utils/utils';

const { TabPane } = Tabs;

// @connect(({ cashManagementReportM, productAGroupM }) => ({
//   cashManagementReportM,
//   productAGroupM,
// }))

class EquityIndustryRisk extends PureComponent {
  constructor() {
    super();
    this.state = {
      activeKey: '1',
      portfolioCode: '',
      hasDataBD: '',
      hasDataED: '',
      // isShow: false,
      // formData: {},
      // childrenMsg: '--'
    };
  }
  componentDidMount() {
    const {
      dispatch,
      globalM: {
        dataDictionary: { MARKET_RISK_STK_BENCHMARK = [], MARKET_RISK_STK_INDU_CATEGORY = [] },
      },
    } = this.props;
    // 基准和行业分类字典
    dispatch({
      type: 'equityIndustryRiskM/save',
      payload: {
        dictionary: {
          benchMarkList: formSelectData(MARKET_RISK_STK_BENCHMARK),
          industryTypeList: MARKET_RISK_STK_INDU_CATEGORY,
        },
      },
    });
  }

  //获取产品时间范围
  getProductTime = (portfolioCode, timeRange) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'equityIndustryRiskM/findProductTime',
      payload: {
        portfolioCode: portfolioCode,
        timeRange: timeRange === '0' ? 6 : timeRange,
      },
      callback: (res) => {
        const data = res.data;
        if (timeRange === '0') {
          this.state.COMPLEXDATE?.setSelctAndDate(timeRange, '', '');
          this.setState({
            hasDataBD: moment(data.beginDate),
            hasDataED: moment(data.endDate),
          });
        } else {
          this.state.COMPLEXDATE.setSelctAndDate(timeRange, moment(data.beginDate), moment(data.endDate));
        }
      },
    });
  };

  //时间范围类型改变
  selOnChange = (value) => {
    this.getProductTime(this.state.portfolioCode, value);
    this.reastSearchData();
  };

  //组合和产品改变
  handPortfolioCodeChange = (e) => {
    if (e) {
      const info = e.info;
      // const { dispatch } = this.props;
      this.setState({ portfolioCode: info.portfolioCode });
      this.getProductTime(info.portfolioCode, '4');
    } else {
    }
    this.reastSearchData();
  };

  reastSearchData = () => {
    const { equityIndustryRiskM } = this.props;

    if (JSON.stringify(equityIndustryRiskM.searchData) !== '{}') {
      this.props.dispatch({
        type: 'equityIndustryRiskM/save',
        payload: {
          searchData: null,
        },
      });
    }
  };

  // 查询
  searchData = (e) => {
    const { dispatch } = this.props;
    const { portfolioCode } = this.state;
    const { benchMark, beginDate, endDate } = e;

    if (!portfolioCode) {
      message.warning('请选择产品');
      return;
    }
    if (!benchMark) {
      message.warning('请选择基准');
      return;
    }
    if (!beginDate && !endDate) {
      message.warning('请选择时间区间');
      return;
    }

    dispatch({
      type: 'equityIndustryRiskM/save',
      payload: {
        searchData: {
          portfolioCode,
          benchMark,
          beginDate: beginDate.format('YYYY-MM-DD'),
          endDate: endDate.format('YYYY-MM-DD'),
        },
      },
    });
  };

  render() {
    const { equityIndustryRiskM } = this.props;

    const searchProps = {
      dataSource: [
        {
          dataIndex: 'portfolioCode',
          title: '产品名称',
          type: DATA_ENTRY.PRODUCTGROUPTREE,
          multiple: false,
          allowClear: true,
          onChange: (e) => this.handPortfolioCodeChange(e),
        },
        {
          dataIndex: 'benchMark',
          title: '基准',
          type: DATA_ENTRY.SELECT,
          dataSource: equityIndustryRiskM.dictionary?.benchMarkList,
          onChange: () => this.reastSearchData(),
          // allowClear: true,
        },
        {
          selectIndex: 'time',
          startIndex: 'beginDate',
          endIndex: 'endDate',
          title: '时间区间',
          type: DATA_ENTRY.COMPLEXDATE,
          hasDataBD: this.state.hasDataBD,
          hasDataED: this.state.hasDataED,
          disabled: !this.state.portfolioCode,
          selOnChange: this.selOnChange,
          onRef: (ref) => this.setState({ COMPLEXDATE: ref }),
        },
      ],
      onQuery: (e) => this.searchData(e),
      onClear: () => {
        this.setState({ portfolioCode: '' });
        this.reastSearchData();
      },
    };
    return (
      <ContentPackage
        title='权益行业风险分析'
        contentHtml={
          <div>
            <div>
              <AdvancedSearch {...searchProps} />
            </div>
            <div style={{ marginTop: '10px' }}>
              <Tabs activeKey={this.state.activeKey}>
                <TabPane tab='行业敞口分析' key='1'>
                  {JSON.stringify(equityIndustryRiskM.searchData) !== '{}' ? <IndustryExposure /> : <NoQueryC></NoQueryC>}
                </TabPane>
              </Tabs>
            </div>
          </div>
        }
      />
    );
  }
}
export default connect(({ equityIndustryRiskM, globalM }) => ({
  equityIndustryRiskM,
  globalM,
}))(EquityIndustryRisk);
